package com.xry.service.impl;

import com.xry.annotation.LogMethod;
import com.xry.bean.Category;
import com.xry.mapper.CategoryDao;
import com.xry.mapper.ProductDao;
import com.xry.service.CategoryService;
import com.xry.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    ProductDao productDao;
    @Override
    @LogMethod(serviceName = "增加分类信息")
    public Category add(Category category) {
        //新增数据
        categoryDao.add(category);
        //通过主键查询数据库中的数据
        Category result = categoryDao.one(category.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除分类信息")
    @Transactional
    public int del(Integer id) {
        productDao.delByCategory(id);
        return categoryDao.del(id);
    }

    @Override
    @LogMethod(serviceName = "修改分类信息")
    public Category edit(Category category) {
        categoryDao.edit(category);
        Category result=categoryDao.one(category.getId());
        return result;
    }

    @Override
    public List<Category> all(Category category) {
        return categoryDao.all( category);
    }

    @Override
    public Category one(Integer id) {
        return categoryDao.one(id);
    }

    @Override
    public void page(Page page, Category category){
        int count=categoryDao.pageCount(category);
        Object data=categoryDao.page(page,category);
        page.setCount(count);
        page.setData(data);
    }
}